package com.itfreer.ui.base.domain;

import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Map;

import com.itfreer.ui.R;
import com.itfreer.ui.base.DialogCloseListener;
import com.itfreer.ui.base.DialogResult;

/**
 * 定义单选弹出框
 */
public class DomainRadioDialog extends AlertDialog implements DialogInterface.OnClickListener {

    /**
     * 对话框标题
     */
    private String dialogTitle;

    /**
     * 适配器
     */
    private DomainRadioAdapter adapter;
    private ListView listView;
    private View baseView;
    private TextView title;

    /**
     * 对话框返回值
     */
    private DialogResult dialogResult = DialogResult.Cancel;
    private DialogCloseListener dialogCloseListener;
    private Object value;
    private Object bindData;

    /**
     * 设置当前选择项
     * @param value
     */
    public void setCheckedValue(Object value){
        this.value = value;
        if(adapter!=null){
            adapter.setCheckedValue(value);
        }
    }

    /**
     * 获取值
     * @return
     */
    public Object getCheckedValue(){
        return value;
    }

    /**
     * 获取返回值
     * @return
     */
    public DialogResult getDialogResult(){
        return dialogResult;
    }

    /**
     * 关闭事件
     * @param dialogCloseListener
     */
    public void setDialogCloseListener(DialogCloseListener dialogCloseListener){
        this.dialogCloseListener = dialogCloseListener;
    }

    /**
     * 设置绑定的数据
     * @param data
     */
    public void setBindData(Object data){
        bindData = data;
        Map domain = null;
        if(bindData instanceof Map){
            domain = (Map)bindData;
        }
        adapter = new DomainRadioAdapter(baseView.getContext(), domain);
        listView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView);
    }

    /**
     * 设置标题
     * @param dialogTitle
     */
    public void setDialogTitle(String dialogTitle){
        this.dialogTitle = dialogTitle;
        title.setText(dialogTitle);
    }

    public String getDialogTitle(){
        return dialogTitle;
    }

    public DomainRadioDialog(Context context) {
        super(context, 0);

        Context themeContext = getContext();
        setButton(BUTTON_POSITIVE, "确 定", this);
        setButton(BUTTON_NEGATIVE, "取 消", this);
        setIcon(0);

        LayoutInflater inflater = (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        baseView = inflater.inflate(R.layout.com_itfreer_core_ui_dialog_domaindialog, null);
        setView(baseView);

        title = (TextView) baseView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_dialogTitle);
        listView = (ListView) baseView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_listview);
    }

    public void onClick(DialogInterface dialog, int which) {
        if (which == BUTTON_POSITIVE){
            dialogResult = DialogResult.Ok;
            if(adapter!=null) {
                value = adapter.getCheckedValue();
            }
            if(dialogCloseListener!=null){
                dialogCloseListener.OnClosed(this, dialogResult);
            }
            this.dismiss();
        } else {
            dialogResult = DialogResult.Cancel;
            if(adapter!=null){
                adapter.setCheckedValue(value);
            }
            if(dialogCloseListener!=null){
                dialogCloseListener.OnClosed(this, dialogResult);
            }
            this.dismiss();
        }
    }

    private void setListViewHeightBasedOnChildren(ListView listView) {
        Adapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        int viewCount = listAdapter.getCount();
        for (int i = 0; i < viewCount; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        totalHeight += (listView.getDividerHeight() * (listAdapter.getCount()-1)) + 10;//加10是为了适配自定义背景
        if(totalHeight>1500){
            totalHeight = 1500;
        }
        params.height = totalHeight;
        listView.setLayoutParams(params);
    }
}